ggPedigree()This vignette demonstrates some of the more complex family trees you
can visualization with ggPedigree().
We illustrate usage with a more complicated data set from BGmisc:
asoiaf – extended Targaryen family# Install your package from GitHub
library(ggpedigree) # ggPedigree lives here
library(BGmisc) # helper utilities & example data
library(ggplot2) # ggplot2 for plotting
library(viridis) # viridis for color palettes
library(tidyverse) # for data wranglingWe begin by loading the required libraries and examining the
structure of the built-in ASOIAF pedigree.
The ASOIAF dataset includes character IDs, names, family identifiers, and parent identifiers for a subset of characters drawn from the A Song of Ice and Fire canon.
df_got <- checkSex(ASOIAF,
code_male = "M",
code_female = "F",
repair = TRUE
)
# Find the IDs of Jon Snow and Daenerys Targaryen
jon_id <- df_got %>%
filter(name == "Jon Snow") %>%
pull(ID)
dany_id <- df_got %>%
filter(name == "Daenerys Targaryen") %>%
pull(ID)Here we’ve added phantom IDs to the dataset, which are used to represent individuals who are not directly related to the family tree but are still part of the pedigree structure. This is useful for visualizing complex relationships, such as half-siblings or step-siblings.
df_repaired <- checkParentIDs(df_got,
addphantoms = TRUE,
repair = TRUE,
parentswithoutrow = FALSE,
repairsex = FALSE
) %>% mutate(
famID = 1,
affected = case_when(
ID %in% c(jon_id, dany_id, "365") ~ 1,
TRUE ~ 0
)
)
#> REPAIR IN EARLY ALPHAplotPedigree()Here is the classic pedigree plot of the Targaryen family, with Jon
Snow and Daenerys Targaryen highlighted in black The
plotPedigree() function from {BGmisc} provides a quick way
to visualize the pedigree structure. It serves as a wrapper function
from {kinship2} and is useful for quickly checking the pedigree
structure.
#> Did not plot the following people: 85 88 125 142 228 229 258 259 274 275 305 336 357 381 388 405 409 418 420 424 428 451 487
#> named list()
ggPedigree()Here is the same pedigree, but using ggPedigree() from
{ggpedigree}. This function provides a more flexible and customizable
way to visualize pedigrees, allowing for easy integration with other
ggplot2 functions.
pltstatic <- ggPedigree(df_repaired,
status_column = "affected",
personID = "ID",
config = list(
return_static = TRUE,
status_label_unaffected = 0,
sex_color_include = TRUE,
code_male = "M",
point_size=1,
status_label_affected = 1,
status_affected_shape = 4,
ped_width = 14,
tooltip_include = TRUE,
label_nudge_y = -.25,
label_include = TRUE,
label_method = "geom_text",
segment_self_color = "purple",
tooltip_columns = c("ID", "name")
)
)
pltstaticplt <- ggPedigreeInteractive(df_repaired,
status_column = "affected",
personID = "ID",
config = list(
status_label_unaffected = 0,
sex_color_include = TRUE,
code_male = "M",
point_size = 1,
status_label_affected = 1,
status_affected_shape = 4,
ped_width = 14,
segment_self_angle = 90,
segment_self_curvature = -0.2,
tooltip_include = TRUE,
label_nudge_y = -.25,
label_include = TRUE,
label_method = "geom_text",
segment_self_color = "purple",
tooltip_columns = c("ID", "name")
)
)
plt